package com.pifuke.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
import cn.o.app.adapter.IItemView;
import cn.o.app.adapter.OAdapter;
import cn.o.app.adapter.OItemView;
import cn.o.app.json.JsonUtil;
import cn.o.app.ui.FlowLayout;
import cn.o.app.ui.OImageView;
import cn.skinapp.R;

import com.lidroid.xutils.BitmapUtils;
import com.smiier.skin.DoctorDetailActivity;
import com.smiier.skin.constant.Constant;
import com.smiier.skin.net.entity.Hospital.HospitalItem;
import com.smiier.skin.net.entity.User;
import com.smiier.skin.net.entity.UserWrapper;
import com.smiier.skin.ui.BasicActivity;
import com.smiier.skin.util.GlobalSettings;
import com.smiier.skin.utils.CommonUtility;

public class UserAdapter extends OAdapter<UserWrapper> {
	protected BitmapUtils mBitmapUtils;
	private boolean isDoctor = true;
	private BasicActivity mActivity;
	User mCheckUser;

	@Override
	protected void onContainerChanged() {
		mBitmapUtils = new BitmapUtils(getContext());
	}

	public void setActivity(BasicActivity activity) {
		this.mActivity = activity;
	}

	public void setIsDoctor(boolean isDoctor) {
		this.isDoctor = isDoctor;
	}

	public void setUser(User mCheckUser) {
		this.mCheckUser = mCheckUser;
	}

	@Override
	public IItemView<UserWrapper> getItemView() {
		return new DoctorUserView(getContext());
	}

	class DoctorUserView extends OItemView<UserWrapper> {
		OImageView img;
		TextView text_doctor_name, text_doctor_hospital, text_doctor_title,
				text_charge;
		FlowLayout mDoctorSkill;
		TextView mHosLevel;
		TextView mProjectNum;
		OImageView mCheckImage;

		public DoctorUserView(Context context) {
			super(context);
		}

		@Override
		public void onCreate() {

			setContentView(R.layout.item_doctor);
			img = findViewById(R.id.imgLayout_show, OImageView.class);
			text_doctor_name = findViewById(R.id.text_doctor_name,
					TextView.class);
			mCheckImage = findViewById(R.id.imgLayout_check, OImageView.class);
			mHosLevel = findViewById(R.id.treatment_hospital_level,
					TextView.class);
			mDoctorSkill = findViewById(R.id.text_doctor_skill,
					FlowLayout.class);
			text_doctor_hospital = findViewById(R.id.text_doctor_hospital,
					TextView.class);
			text_doctor_title = findViewById(R.id.text_doctor_title,
					TextView.class);
			mProjectNum = findViewById(R.id.text_treatment_num, TextView.class);
			text_charge = findViewById(R.id.text_charge, TextView.class);
			img.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(getContext(),
							DoctorDetailActivity.class);
					try {
						intent.putExtra(Constant.IDENTITY_KEY,
								JsonUtil.convert(mDataProvider.User));
						getContext().startActivity(intent);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});
		}

		@Override
		public void onResume() {
			try {
				text_doctor_name.setText(mDataProvider.User.Name);
				boolean hasName = false;
				CommonUtility.setHosLevel(mDataProvider.User.Hospital,
						mHosLevel);
				if (!hasName) {
					mHosLevel.setVisibility(View.GONE);
				}
				if (mDataProvider.User.project_count > 0) {
					mProjectNum.setVisibility(View.VISIBLE);
					mProjectNum.setText(mDataProvider.User.project_count
							+ "个线下治疗项目");
				} else {
					mProjectNum.setVisibility(View.GONE);
				}

				if (mCheckUser != null
						&& (mCheckUser.Uid + "").equals(mDataProvider.User.Uid
								+ "")) {
					mCheckImage.setVisibility(View.VISIBLE);
				} else {
					mCheckImage.setVisibility(View.GONE);
				}
				if (isDoctor) {
					mDoctorSkill.setVisibility(View.VISIBLE);
					// viewHolder.mDoctorSkill.setText(CommonUtility
					// .convertJSONArray2String(
					// doctor.getJSONArray("Skilled"), " | "));
					mDoctorSkill.removeAllViews();
					List<String> array = mDataProvider.User.Skilled;
					for (String str : array) {
						if (str != null && !str.equals("")) {
							CheckBox button = (CheckBox) LayoutInflater.from(
									mActivity).inflate(
									R.layout.item_filter_button, null);
							button.setEnabled(false);
							button.setOnClickListener(new OnClickListener() {

								@Override
								public void onClick(View v) {

								}
							});
							button.setText(str);
							mDoctorSkill.addView(button);
						}
					}
					List<String> prof = mDataProvider.User.Professional;
					StringBuilder sb = new StringBuilder();
					for (String str : prof) {
						sb.append(str).append(",");
					}
					text_doctor_title.setText(sb.replace(sb.length() - 1,
							sb.length(), "").toString());
					String hos = mDataProvider.User.Hospital;
					text_doctor_hospital.setText(hos);
					for (HospitalItem item : GlobalSettings.mHospitalItems) {
						if (hos != null && !hos.equals("")) {
							if (item.Name.equals(hos)
									|| item.FullName.equals(hos)
									|| item.Name.contains(hos)
									|| item.FullName.contains(hos)
									|| hos.contains(item.Name)
									|| hos.contains(item.FullName)) {
								if (item.Level != null
										&& !item.Level.equals("")) {
									text_doctor_hospital.setText(item.Name);
								}
							}
						}
					}
					sb = new StringBuilder();
					double cost = mDataProvider.User.Cost;
					String charge = "图文咨询  免费";
					if (cost > 0) {
						charge = cost + "";
						if (charge.endsWith(".0")) {
							charge = charge.substring(0, charge.length() - 2);
						}
						sb.append("图文咨询").append("￥").append(charge);
					} else {
						sb.append(charge);
					}
					text_charge.setText(sb);
				} else {
					text_charge.setVisibility(View.GONE);
					mDoctorSkill.setVisibility(View.GONE);
					mProjectNum.setVisibility(View.GONE);
					StringBuilder sb = new StringBuilder();
					String sex = CommonUtility.sex.get(mDataProvider.User.Sex);
					String age = CommonUtility
							.getAgeByBirthday(mDataProvider.User.Birth
									.toLocaleString());
					String nation = mDataProvider.User.Nation;
					String address = mDataProvider.User.City;
					String marry = CommonUtility.marry
							.get(mDataProvider.User.Marital_Status);
					sb.append(sex);
					if (!CommonUtility.isNull(age)) {
						sb.append("，");
					}
					sb.append(age + "岁");
					text_doctor_hospital.setText(sb.toString());

					// viewHolder.mDoctorSkill.setText(sb);
				}
				String iconPath = mDataProvider.User.Pic;
				if (iconPath.indexOf(".") != -1) {
					iconPath = GlobalSettings.SERVER_IMG_URL + iconPath;
				} else {
					iconPath = GlobalSettings.SERVER_IMG_URL + iconPath
							+ Constant.IMG_TH;
				}
				// mBitmapUtils.display(img, iconPath,
				// new OBitmapLoadCallBack<View>());
				CommonUtility.displayHeadImage(img, mBitmapUtils, iconPath,
						getContext().getResources(), mDataProvider.User.Sex);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}

}
